﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using ShredTheLog.Api;

namespace ShredTheLog.Converters
{
    [ValueConversion(typeof(bool?), typeof(object))]
    public sealed class NullableBoolConverter : ConverterBase, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var v = (bool?)value;
            return v.HasValue ? (object)(v.Value ? YesNo.Yes : YesNo.No) : (object)new NullValue();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is YesNo)
            {
                switch ((YesNo)value)
                {
                    case YesNo.Yes: return (bool?)true;
                    case YesNo.No: return (bool?)false;
                    default: throw new NotSupportedException();
                }
            }
            else if (value is NullValue)
                return (bool?)null;
            else
                throw new NotSupportedException();
        }

        public static readonly NullableBoolConverter Default = new NullableBoolConverter();
    }
}
